# Rebel Commitment
# Replicate Figures 1 through 4 

rm(list=ls())

#=================================#
# Install and Load packages (To replicate the figures, please install the following packages.)
#=================================#

# If you already installed these packages, it's fine to skip this line.
# This process takes a while (about 5-10 mins.)
install.packages(c("haven", "ggplot2", "dplyr"))

# Load the packages
library(haven)
library(ggplot2)
library(dplyr)



#=================================#
# Set working directory to the main folder
#=================================#

setwd("[the main folder]")


#=================================#
# Figure 1
#=================================#

# Load data
sample <- read_dta("commitment_document.dta")

# Look at commitments by year: time trend
annual.sum <- sample %>%
  group_by(year) %>%
  summarize(
    groups = n(),
    commitments = sum(num_document)
  )

# Scatter plot and smooth to show trend
ggplot(annual.sum, aes(x = year, y = commitments)) + 
  geom_point() + geom_smooth(alpha=0.25, colour="black") +
  theme_classic() + labs(x = "Year", y = "Rebel Commitments to International Law") +
  ggtitle("")
ggsave("figures/figure1.tiff")



#=================================#
# Figure 2
#=================================#

# Load the estimates of predicted probabilities

pr.table.sup_nsa <- read.csv("figures/psup.csv")
pr.table.sup_nsa$supnsa <- c("x1", "x2")

p <- ggplot(pr.table.sup_nsa, aes(y=mean, x=supnsa)) +
  geom_point(size = 2) + geom_errorbar(aes(ymax = ub95, ymin = lb95), width = 0.05, size = 0.3) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  ggtitle("") +
  ylab("Predicted Probability of Commitment") + xlab("Non-military Support by Transnational Non-state Actors") + 
  scale_x_discrete("Non-military Support by Transnational Non-state Actors", 
                   labels = c("x1" = "No", "x2" = "Yes")) +
  theme_bw() +
  annotate("text", x=1.1, y=.052, label = "0.05") + annotate("text", x=2.1, y=.106, label = "0.11")


plot.pr.sup_nsa <- p + geom_col(aes(y=obs/10000), alpha = 0.12) +
  scale_y_continuous(sec.axis = sec_axis(~.*10000, breaks = c(0, 500, 1000), name = "Number of Observations"))

plot.pr.sup_nsa
ggsave("figures/figure2.tiff")




#=================================#
# Figure 3
#=================================#

# Load the estimates of predicted probabilities
pr.table.ccs <- read.csv("figures/pccs.csv")
pr.table.ccs$ccs <- c("x1", "x2", "x3", "x4")

p <- ggplot(pr.table.ccs, aes(y=mean, x=ccs)) +
  geom_point(size = 2) + geom_errorbar(aes(ymax = ub95, ymin = lb95), width = 0.05, size = 0.3) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  ggtitle("") +
  ylab("Predicted Probability of Commitment") + xlab("Central Control Strength") + 
  scale_x_discrete("Central Control Strength", 
                   labels = c("x1" = "No", "x2" = "Low",
                              "x3" = "Moderate", "x4" = "High")) +
  theme_bw() +
  annotate("text", x=1.2, y=0.026, label = "0.03") + annotate("text", x=2.2, y=.043, label = "0.04") +
  annotate("text", x=3.2, y=.0705, label = "0.07") + annotate("text", x=4.2, y=.1109, label = "0.11")


plot.pr.ccs <- p + geom_col(aes(y=obs/7000), alpha = 0.12) +
  scale_y_continuous(sec.axis = sec_axis(~.*7000, breaks = c(0, 500, 1000), name = "Number of Observations"))

plot.pr.ccs
ggsave("figures/figure3.tiff")





#=================================#
# Figure 4
#=================================#

# Load the estimates of predicted probabilities
pr.table.peace <- read.csv("figures/pnpt.csv")
pr.table.peace$peace <- c("x1", "x2")  

p <- ggplot(pr.table.peace, aes(y=mean, x=peace)) +
  geom_point(size = 2) + geom_errorbar(aes(ymax = ub95, ymin = lb95), width = 0.05, size = 0.3) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  ggtitle("") +
  ylab("Predicted Probability of Commitment") + xlab("Near Peace talks") + 
  scale_x_discrete("Near Peace talks", 
                   labels = c("x1" = "No", "x2" = "Yes")) +
  theme_bw() +
  annotate("text", x=1.1, y=.047, label = "0.05") + annotate("text", x=2.1, y=.17, label = "0.17")


plot.pr.peace <- p + geom_col(aes(y=obs/6000), alpha = 0.12) +
  scale_y_continuous(sec.axis = sec_axis(~.*6000, breaks = c(0, 700, 1500), name = "Number of Observations"))

plot.pr.peace
ggsave("figures/figure4.tiff")
